package com.bzy.shiro.session;

import java.io.Serializable;
import java.util.Collection;

import com.bzy.shiro.service.ISessionService;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.session.mgt.eis.AbstractSessionDAO;


public class CustomAbstractSessionDAO extends AbstractSessionDAO {

	private ISessionService sessionService;

	private String keyPattern;

	public void setSessionService(ISessionService sessionService) {
		this.sessionService = sessionService;
	}

	public void setKeyPattern(String keyPattern) {
		this.keyPattern = keyPattern;
	}

	@Override
	public void update(Session session) throws UnknownSessionException {
		// TODO Auto-generated method stub
		try {
			sessionService.doUpdate(session, keyPattern);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void delete(Session session) {
		// TODO Auto-generated method stub
		try {
			sessionService.doDelete(session, keyPattern);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public Collection<Session> getActiveSessions() {
		// TODO Auto-generated method stub
		Collection<Session> Sessions = null;
		try {
			Sessions = sessionService.getAllSessions(keyPattern);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return Sessions;
	}

	@Override
	protected Serializable doCreate(Session session) {
		// TODO Auto-generated method stub
		Serializable sessionId = generateSessionId(session);
		assignSessionId(session, sessionId);
		try {
			sessionService.doCreate(session, keyPattern);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return session.getId();
	}

	@Override
	protected Session doReadSession(Serializable sessionId) {
		// TODO Auto-generated method stub
		Session session = null;
		try {
			session = sessionService.doReadSession(sessionId, keyPattern);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return session;
	}

}
